//
//  DataBaseHandle.m
//  ALearningWalk
//
//  Created by 张勇 on 16/7/2.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "DataBaseHandle.h"

@interface DataBaseHandle ()

@property (nonatomic, strong) FMDatabaseQueue *myQueue;

@end

@implementation DataBaseHandle

//1. 先创建单例
//2. 将FMDataBase写成全局属性
//3. 导入数据库的类库  libsqlite3.0.tbd
//4. .h里   引入头文件(#import <sqlite3.h> , #import "FMDB.h")创建数据库对象

//单例
+(DataBaseHandle *)shareDataFMDBHandle{
    
    static DataBaseHandle *data = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        data = [[DataBaseHandle alloc]init];
    });
    
    return data;
}

//打开数据库
-(void)openDB{
    
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    
    NSLog(@"%@", path);
    NSString *dataPath = [path stringByAppendingPathComponent:@"BZBT.sqlite"];
    
    self.myQueue = [FMDatabaseQueue databaseQueueWithPath:dataPath];
    
    [self.myQueue inDatabase:^(FMDatabase *db) {
        
        BOOL result = [db open];
        if (result) {
            
            NSLog(@"数据库打开成功");
        }else{
            
            NSLog(@"数据库打开失败");
        }
        
    }];
    
}

//创建表
-(void)creatTable{
    
    //下面的写法更安全,同时访问时会自动先后进入方法(线程安全)
    [self.myQueue inDatabase:^(FMDatabase *db) {
        
        BOOL result = [db executeUpdate:@"create table if not exists ALearningWalk (id integer primary key autoincrement, albumCoverUrl290 text, albumid text, title text)"];
        if (result) {
            NSLog(@"创建表成功");
        }else{
            
            NSLog(@"创建表失败");
        }
        
    }];
}

//插入数据
-(void)insertTravelsCollectTableWithModel:(smallListModel *)Model{
    
    [self.myQueue inDatabase:^(FMDatabase *db) {
        
        NSString *sql = [NSString stringWithFormat:@"insert into ALearningWalk (albumCoverUrl290, albumid, title) values('%@', '%@', '%@')",Model.albumCoverUrl290,Model.albumId, Model.title];
        
        BOOL result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"插入收藏表数据成功");
        }else{
            
            NSLog(@"插入收藏表数据失败");
        }
        
    }];
    
}


//查询所有
-(NSMutableArray *)selectAll{
    
    NSMutableArray *DataArray = [NSMutableArray array];
    
    [self.myQueue inDatabase:^(FMDatabase *db) {
        
        NSString *sql = @"select * from ALearningWalk";
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            
            smallListModel *model = [[smallListModel alloc] init];
            
            model.albumCoverUrl290 = [result objectForColumnName:@"albumCoverUrl290"];
            
            model.albumId = [result objectForColumnName:@"albumid"];
            
            model.title = [result objectForColumnName:@"title"];
            
            NSLog(@"%@ %@ %@",model.albumCoverUrl290,model.albumId,model.title);
            
            [DataArray addObject:model];
        }
       
    }];
    NSLog(@"dataArray === %@",DataArray);
    return DataArray;
}

//条件删除
-(void)deleteTableWithalbumid:(NSString *)albumid {
    
    [self.myQueue inDatabase:^(FMDatabase *db) {
        
        NSString *sql = [NSString stringWithFormat:@"delete from ALearningWalk where albumid = '%@'",albumid];
        
        BOOL result = [db executeUpdate:sql];
        if (result) {
            
            NSLog(@"删除收藏表数据成功");
        }else{
            
            NSLog(@"删除收藏表数据失败");
        }
        
    }];
    
}

@end
